<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>职责链</title>
  </head>
  <body></body>
</html>
<script>
  function loadImg(src) {
    const promise = new Promise((resolve, reject) => {
      const img = document.createElement("img");
      img.onload = () => {
        resolve(img);
      };
      img.onerror = () => {
        reject("图片加载失败");
      };
      img.src = src;
    });
    return promise;
  }

  const src = "//www.baidu.com/img/flexible/logo/pc/result.png";
  loadImg(src)
    .then((img) => {
      document.body.appendChild(img);
      return img;
    })
    .then((img) => {
      console.log("img.width", img.width);
      return img;
    })
    .then((img) => {
      console.log("img.height", img.height);
    })
    .catch((err) => {
      console.log(err);
    });
</script>
